package com.fu.edu;

import com.fu.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 付大大
 * @create 2021-01-04 18:19
 */
public class StudentManager {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();
        lo:
        while (true) {
            //1.搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = scanner.next();

            switch (choice) {
                case "1":
                    System.out.println("添加学生");
                    addStudent(students);

                    break;
                case "2":
                    deleteStudents(students);
                    break;
                case "3":
                    updateStudents(students);
                    break;
                case "4":
                    queryStudents(students);

                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    public static void updateStudents(ArrayList<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要修改的学号");
        String id = scanner.next();
        int index = getIndex(students, id);

        if (index == -1) {
            System.out.println("查无信息，请重新输入");
        } else {
            System.out.println("请输入新的学生姓名");
            String name = scanner.next();
            System.out.println("请输入新的学生年龄");
            int aeg = scanner.nextInt();
            System.out.println("请输入新的学生生日");
            String birthday = scanner.next();

            Student student = new Student(id, name, aeg, birthday);
            students.set(index, student);
            System.out.println("修改成功");
        }
    }

    public static void deleteStudents(ArrayList<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要删除的学号");
        String id = scanner.next();
        int index = getIndex(students, id);

        if (index == -1) {
            System.out.println("查无信息，请重新输入");
        } else {
            students.remove(index);
            System.out.println("删除成功");
        }

    }

    /**
     *     查询学生方法
     *
     */
    public static void queryStudents(ArrayList<Student> students) {
        if (students.size() == 0) {
            System.out.println("无信息，请添加后重新查询");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getBirthday());
        }

    }

    /**
     *
     * @param students//添加学生方法
     */
    public static void addStudent(ArrayList<Student> students) {
        String sid;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学号");
            sid = scanner.next();
            int index = getIndex(students, sid);
            if (index == -1) {
                break;
            }
            System.out.println("已经存在该学号");
        }
        System.out.println("请输入姓名");
        String name = scanner.next();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        System.out.println("请输入生日");
        String birthday = scanner.next();
        Student student = new Student(sid, name, age, birthday);
        students.add(student);
        System.out.println("添加成功");
    }



    public static int getIndex(ArrayList<Student> students, String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            String id1 = student.getId();
            if (id1.equals(id)) {
                index = i;
            }
        }
        return index;
    }

}
